//
//  QueRenViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/8/10.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "QueRenViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "QueRenCell.h"
#import "BuySuccViewController.h"
#import <CoreText/CoreText.h>
#import "WeiXinPayManager.h"
#import "AliPayManager.h"
#import "BuyCllowerCell.h"
#import "AddressViewController.h"
#import "IGAddressItem.h"
#import "IGSubmitGoodsApi.h"
#import "IGPaymentGoodsResultApi.h"
#import "OrderXQViewController.h"
#import "GoodsDetailVC.h"
#import "OrderNumberCell.h"

@interface QueRenViewController ()<SwitchChangeDelegate,UITextFieldDelegate>
{
    UITextField *numberTV;//购买件数
    NSInteger buyCount;//购买数量

    NSInteger _payType;//1支付宝 2微信
    NSInteger art_type;// 1单独支付 2混合支付
    NSString *orderId;// 订单id
    NSString *ifUseArtPoint;//是否使用艺点,1使用, 0不适用
    IGAddressItem *addressItem;
}
@property (nonatomic, strong)UIView *buyView;
@property (nonatomic, strong)UILabel *moneyL;
@property (nonatomic, copy)NSString *artCion;//艺点
@property (nonatomic, strong) NSMutableArray *addressArr;
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *table;

@end

@implementation QueRenViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title =  NSLocalizedString(@"Authenticated Works Purchasing", nil);
    _payType = (_type == 2 ) ? 1 : 1;
    art_type = 1;
    buyCount = 1;
    self.table.backgroundColor =[UIColor c2Color];
    self.table.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.table.contentInset = UIEdgeInsetsMake(0, 0, 44, 0);
    self.table.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 44, 0);
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-44);
        make.top.mas_equalTo(0);
    }];
    [self.table registerNib:[UINib nibWithNibName:@"QueRenCell" bundle:nil] forCellReuseIdentifier:@"QueRenCell"];
    [self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.table registerNib:[UINib nibWithNibName:@"OrderNumberCell" bundle:nil] forCellReuseIdentifier:@"OrderNumberCell"];
    [self.table registerNib:[UINib nibWithNibName:@"BuyCllowerCell" bundle:nil] forCellReuseIdentifier:@"BuyCllowerCell"];
    [self initBuyView];

    //注册监听-支付宝
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dealAlipayResult:) name:@"alipayResult" object:nil];
    //注册监听-微信
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dealWXpayResult:) name:@"WXpayresult" object:nil];
    self.artCion = _model.artPoint;
    [self priceChange];
}

- (void)initBuyView
{
    self.buyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 44)];
    self.buyView.bottom = KSCREENHEIGHT;
    self.buyView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.buyView];
    [self.buyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    self.moneyL = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, KSCREENWIDTH / 3 * 2 - 30, 44)];
    self.moneyL.numberOfLines = 0;
    self.moneyL.textColor = [UIColor c1Color];
    self.moneyL.font = [UIFont systemFontOfSize:18];
    self.moneyL.textAlignment = NSTextAlignmentRight;
    [self.buyView addSubview:self.moneyL];
    [self.moneyL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(KSCREENWIDTH / 3 * 2 - 20);
    }];
    
    UIButton *buy = [UIButton buttonWithType:UIButtonTypeCustom];
    buy.frame = CGRectMake(KSCREENWIDTH / 3 * 2, 0, KSCREENWIDTH / 3, 44);
    buy.bottom = KSCREENHEIGHT;
    buy.backgroundColor = [UIColor c1Color];
    [buy setTitle: NSLocalizedString(@"Go to the payment", nil) forState:UIControlStateNormal];
    [buy setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buy addTarget:self action:@selector(subOrder:) forControlEvents:UIControlEventTouchUpInside];
    buy.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.buyView addSubview:buy];
    [buy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.width.mas_equalTo(KSCREENWIDTH / 3);
    }];
}

- (void)priceChange
{
    if (_type == 2)
    {
//                self.moneyL.text = [NSString stringWithFormat:@"艺点已抵扣: ¥%.2f 应付: ¥%.2f", _model.artPointActual.floatValue/10, _model.priceActual.floatValue];
        self.moneyL.text = [NSString stringWithFormat:@"%@: ¥%.2f",NSLocalizedString(@"Payment", nil), _model.priceActual.floatValue];
        self.moneyL.font = [UIFont font14];
        ifUseArtPoint = @"";
        art_type = 1;
    }
    else
    {
//        BuyCllowerCell *cell = (BuyCllowerCell *)[self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]];
//
//         if (_artCion && [_artCion floatValue] > 0)
//         {
//         //艺点足够时采用单一支付方式
//         if (_artCion.floatValue/10 >= _model.price.floatValue)
//         {
//         ifUseArtPoint = @"1";
//         art_type = 1;
//         cell.textL.text = [NSString stringWithFormat:@"共%.1f艺点, 本次抵扣¥ %.2f", _artCion.floatValue, _model.price.floatValue];
//         }
//         else//艺点不够时采用混合支付方式
//         {
//         art_type = 2;
//         ifUseArtPoint = @"1";
//         cell.textL.text = [NSString stringWithFormat:@"共%.1f艺点, 本次抵扣¥ %.2f", _artCion.floatValue, _artCion.floatValue/10];
//         }
//         cell.switchBtn.on = YES;
//         cell.switchBtn.userInteractionEnabled = YES;
//         }
//         else//无艺点, 第三方支付
//         {
//         art_type = 1;
//         ifUseArtPoint = @"0";
//         cell.textL.text = [NSString stringWithFormat:@"无艺点可用"];
//         cell.switchBtn.on = NO;
//         cell.switchBtn.userInteractionEnabled = NO;
//         }
            
        art_type = 1;
        ifUseArtPoint = @"0";
        //购买数量
        numberTV.text = [NSString stringWithFormat:@"%ld", buyCount];
        self.moneyL.text = [NSString stringWithFormat:@"%@: ¥%.2f", NSLocalizedString(@"Actual Payment", nil),[_model.price floatValue] * buyCount];
//        cell.textL.text = [NSString stringWithFormat:@"无艺点可用"];
//        cell.switchBtn.on = NO;
//        cell.switchBtn.userInteractionEnabled = NO;
//        [self switchIsOn:cell.switchBtn.isOn];
    }    }

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == numberTV)
    {
        
        if ([numberTV.text integerValue] > [_model.stock integerValue] || [numberTV.text integerValue] < 1)
        {
//            [self presentMessageTips:@"订单数量不合法"];
            [self presentMessageTips:NSLocalizedString(@"Illegal Order Amount", nil)];
            buyCount = 1;
            [self priceChange];
        }
        else
        {
            buyCount = [numberTV.text integerValue];
            [self priceChange];
        }
    }
}

- (void)switchIsOn:(BOOL)on
{
    UITableViewCell *cell1 = [self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:2]];
    UITableViewCell *cell2 = [self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:2]];
    if (on)//使用艺点支付
    {
        if (art_type == 1)//艺点足够时采用单一支付方式
        {
            cell1.selectionStyle = UITableViewCellSelectionStyleNone;
            cell2.selectionStyle = UITableViewCellSelectionStyleNone;
            _payType = 0;
            [self.table reloadData];
            self.moneyL.text = [NSString stringWithFormat:@"%@: ¥0.00",NSLocalizedString(@"Actual Payment",nil)];
        }
        else if (art_type == 2)//艺点不够时采用混合支付方式
        {
            cell1.userInteractionEnabled = YES;
            cell2.userInteractionEnabled = YES;
            _payType = 1;
            [self.table reloadData];
            self.moneyL.text = [NSString stringWithFormat:@"%@: ¥%.2f", NSLocalizedString(@"Actual Payment",nil),[_model.price floatValue] - [_artCion floatValue]/10];
        }
        ifUseArtPoint = @"1";
    }
    else
    {
        cell1.userInteractionEnabled = YES;
        cell2.userInteractionEnabled = YES;
        _payType = 1;
        [self.table reloadData];
        self.moneyL.text = [NSString stringWithFormat:@"%@: ¥%.2f", NSLocalizedString(@"Actual Payment",nil),[_model.price floatValue]];
        ifUseArtPoint = @"0";
    }
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 2;
    }
    if (section == 2)
    {
        return 3;
    }
    if (section == 3)
    {
//        return (_type == 2) ? 0 : 1;
        return 0;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CGFloat h = [self tableView:tableView heightForRowAtIndexPath:indexPath];
    if (indexPath.section==0)
    {
        if (addressItem.name && ![addressItem.name isEqualToString:@""])
        {
            cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", addressItem.name, addressItem.tel];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", addressItem.addressNm, addressItem.detail];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        } else {
        if (_model.consignee && ![_model.consignee isEqualToString:@""])
        {
            cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", _model.consignee, _model.phone];
            cell.detailTextLabel.text = _model.detail;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }
        else
        {
            UILabel *address = [[UILabel alloc] initWithFrame:CGRectZero];
            address.numberOfLines = 0;
            address.font = [UIFont systemFontOfSize:15];
            address.lineBreakMode = NSLineBreakByWordWrapping;
//            @"＋ 添加收货地址"
            address.text = NSLocalizedString(@"Add the shipping address", nil);
            [address sizeToFit];
            address.center = CGPointMake(tableView.width/2, h/2);
            [cell.contentView addSubview:address];
            return cell;
        }
        }
    }
    else if (indexPath.section==1)
    {
        if (indexPath.row == 0)
        {
            QueRenCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QueRenCell"];
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:_model.image]];
            cell.titleL.text = _model.title;
            cell.sellerL.text = [NSString stringWithFormat:@"%@:%@",NSLocalizedString(@"Seller", nil),_model.seller];
            cell.numberL.text = [NSString stringWithFormat:@"%@:%@%@",NSLocalizedString(@"Inventory", nil),_model.stock,NSLocalizedString(@"Pieces", nil)];
            cell.priceL.text = [NSString stringWithFormat:@"¥ %@",_model.price];
            return cell;
        } else if (indexPath.row == 1) {
            if (_type == 1) {
                OrderNumberCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderNumberCell" forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                //            cell.titleL.text = @"购买数量";
                cell.titleL.text = NSLocalizedString(@"Purchasing Amount", nil);
                numberTV = cell.numberT;
                numberTV.text = [NSString stringWithFormat:@"%ld", buyCount];
                cell.numberT.delegate = self;
                cell.subBtnClick = ^{
                    if (buyCount > 1)
                    {
                        buyCount--;
                        [self priceChange];
                    }
                    else
                    {
                        //                    [self presentMessageTips:@"订单总量必须大于1"];
                        [self presentMessageTips:NSLocalizedString(@"Order Amount must be above 1", nil)];
                    }
                };
                cell.addBtnClick = ^{
                    if (buyCount < [_model.stock integerValue])
                    {
                        buyCount++;
                        [self priceChange];
                    }
                    else
                    {
                        //                    [self presentMessageTips:@"订单总量必须小于库存"];
                        [self presentMessageTips:NSLocalizedString(@"Order Amount must be below Stock", nil)];
                    }
                };
                return cell;
            }
        }
    }
    else if (indexPath.section==2)
    {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor = [UIColor c4Color];
        CGFloat h = [self tableView:tableView heightForRowAtIndexPath:indexPath];
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 35, 35)];
        imgView.centerY = h/2;
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        
        UIImageView *acc = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        acc.contentMode = UIViewContentModeScaleAspectFit;
        
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectZero];
        title.font = [UIFont systemFontOfSize:15];
        title.textColor = [UIColor c4Color];

        if (indexPath.row==0)
        {
//            cell.textLabel.text = @"选择支付方式";
            cell.textLabel.text = NSLocalizedString(@"Select Payment Method", nil);
        }
        else if (indexPath.row==1)
        {
            imgView.image = [UIImage imageNamed:@"APP buy 切图 20160727-4"];
            [cell.contentView addSubview:imgView];
            title.text = NSLocalizedString(@"Alipay", nil);
            [title sizeToFit];
            [cell.contentView addSubview:title];
            title.centerY = h/2;
            title.left = imgView.right + 18;
            if (_payType == 1)
            {
                acc.image = [UIImage imageNamed:@"未标题-1-2"];
            }
            else
            {
                acc.image = [UIImage imageNamed:@"未标题-1-1"];
            }
            cell.accessoryView = acc;
            UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(title.x, 0, KSCREENWIDTH-title.x, 0.5)];
            line.backgroundColor = [UIColor c6Color];
            [cell.contentView addSubview:line];
        }
        else if (indexPath.row==2)
        {
            imgView.image = [UIImage imageNamed:@"APP buy 切图 20160727-5"];
            [cell.contentView addSubview:imgView];
            title.text = NSLocalizedString(@"WeChat", nil);
            [title sizeToFit];
            [cell.contentView addSubview:title];
            title.centerY = h/2;
            title.left = imgView.right + 18;
            
            if (_payType == 2)
            {
                acc.image = [UIImage imageNamed:@"未标题-1-2"];
            }
            else
            {
                acc.image = [UIImage imageNamed:@"未标题-1-1"];
            }
            cell.accessoryView = acc;
            
            UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(title.x, 0, KSCREENWIDTH-title.x, 0.5)];
            line.backgroundColor = [UIColor c6Color];
            [cell.contentView addSubview:line];
        }
        return cell;
    }
    else if (indexPath.section==3)
    {
        BuyCllowerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BuyCllowerCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.section==4)
    {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, KSCREENWIDTH-30, h)];
        textField.font = [UIFont systemFontOfSize:15];
        textField.keyboardType = UIKeyboardTypeDefault;
        textField.borderStyle = UITextBorderStyleNone;
//        textField.placeholder = @"添加订单备注";
        textField.placeholder = NSLocalizedString(@"Add order notes", nil);
        textField.text = _model.content;
        [textField addTarget:self action:@selector(textChang:) forControlEvents:UIControlEventEditingChanged];
        [cell.contentView addSubview:textField];
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        AddressViewController *vc = [[AddressViewController alloc] init];
        [vc didSelRow:^(IGAddressItem *info) {
            addressItem = info;
            [tableView reloadData];
        }];
        [UIViewController ig_pushViewController:vc animated:YES];
    }
//    else
//    {
//        if (_type == 2)
//        {
            if (indexPath.section == 2 && indexPath.row!=0 )
            {
                _payType = indexPath.row;
                [tableView reloadData];
            }
 //        else
//        {
//            BuyCllowerCell *cell = (BuyCllowerCell *)[self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]];
//            if (cell.switchBtn.isOn && art_type == 1)//使用艺点支付
//            {
//                //无响应
//            }
//            else
//            {
//                if (indexPath.section == 2 && indexPath.row!=0 )
//                {
//                    _payType = indexPath.row;
//                    [tableView reloadData];
//                }
//            }
//        }
//    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return 80;
    }
    else if (indexPath.section==1)
    {
        if (indexPath.row == 0)
        {
            return 100;
        }
        return (_type == 1) ? 55 : 0.0f;
    }
    else if (indexPath.section==2)
    {
        if (indexPath.row==0)
        {
            return 35;
        }
        return 55;
    }
    else if (indexPath.section==3)
    {
        return 55;
    }
    else if(indexPath.section==4)
    {
        return 55;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .01f;
}

- (void)textChang:(UITextField *)text
{
    _model.content = text.text;
}

#pragma mark - 提交订单

- (void)subOrder:(UIButton *)btn
{
    NSString *address, *consignee, *phone;
    if (addressItem.name && ![addressItem.name isEqualToString:@""])
    {
        address = [NSString stringWithFormat:@"%@ %@", addressItem.addressNm, addressItem.detail];
        consignee = addressItem.name;
        phone = addressItem.tel;
    } else {
        if (_model.consignee && ![_model.consignee isEqualToString:@""])
        {
            address = _model.detail;
            consignee = _model.consignee;
            phone = _model.phone;
        }
        else
        {
//            [self presentMessageTips:@"请选择收货地址"];
            [self presentMessageTips:NSLocalizedString(@"Please select the shipping address", nil)];
            return;
        }
    }
    
//    [self showText:@"正在提交订单"];
    [self showText:NSLocalizedString(@"Order Submitting", nil)];
    IGSubmitGoodsApi *api = [IGSubmitGoodsApi new];
    api.orderId = (_type == 2) ? _model.orderId : @"";
    api.goodsId = _model.goodsId;
    api.orderContent = _model.content;
    NSString *str = @"";
    if (_payType == 1)
    {
        str = @"alipay";
    }
    else if (_payType == 2)
    {
        str = @"wxpay";
    }
    api.payPlatCode = str;
    api.ifUseArtPoint = (_type == 2) ? @"" : ifUseArtPoint;
    api.address = address;
    api.consignee = consignee;
    api.phone = phone;
    api.buyNum = [NSString stringWithFormat:@"%ld", buyCount];
    @KWeakObj(self)
    [api startWithDecodeClass:[IGSubmitGoodsResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            [selfWeak dismissLoading];
            IGSubmitGoodsResp *resp = responseObject;
            IGSubmitGoodsModel *submitOrderModel = resp.data;
            if ([submitOrderModel.method isEqualToString:@"paySuccess"])
            {
                BuySuccViewController *vc = [[BuySuccViewController alloc] initWithNibName:@"BuySuccViewController" bundle:nil];
                @KWeakObj(vc)
                orderId = submitOrderModel.orderId;
                vc.orderId = orderId;
                vc.backBtnClick = ^{
                    [UIViewController ig_popViewController:[GoodsDetailVC class]];
                };
                vc.orderBtnClick = ^{
                    [vcWeak back];
                    OrderXQViewController *vc1 = [[OrderXQViewController alloc] init];
                    vc1.oid = orderId;
                    vc1.isMyBuy = YES;
                    [UIViewController ig_pushViewController:vc1 animated:YES];
                };
                [selfWeak presentViewController:vc animated:YES completion:nil];
            }
            else
            {
                orderId = submitOrderModel.orderId;
                if (_payType == 1)
                {
                    [[AliPayManager sharedAliPayManager] payWithDic:submitOrderModel.ResBean];
                    return;
                }
                else if (_payType == 2)
                {
                    NSDictionary *dic = submitOrderModel.ResBean[@"data"];
                    if (dic)
                    {
                        WXProduct * wx  = [WXProduct new];
                        wx.package = dic[@"package"];
                        wx.noncestr = dic[@"noncestr"];
                        wx.appid = dic[@"appid"];
                        wx.partnerid = dic[@"partnerid"];
                        wx.prepayid = dic[@"prepayid"];
                        wx.timestamp = [dic[@"timestamp"] longLongValue];
                        wx.sign = dic[@"sign"];
                        [[WeiXinPayManager sharedManager]weiXinPay:wx];
                        return;
                    }
                }
            }
        }
    }];
    
}

#pragma mark - 支付宝支付状态回调
-(void)dealAlipayResult:(NSNotification*)notification{
    NSString*result=notification.object;
    if([result isEqualToString:@"9000"]){
        //在这里写支付成功之后的回调操作
        NSLog(@"支付宝支付成功");
        IGPaymentGoodsResultApi *api = [IGPaymentGoodsResultApi new];
        api.orderId = orderId;
        api.goodsId = _model.goodsId;
        api.payPlatCode = @"alipay";
        [api startWithDecodeClass:[IGPaymentGoodsResultesp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            
        }];
        @KWeakObj(self)
        BuySuccViewController *vc = [[BuySuccViewController alloc] initWithNibName:@"BuySuccViewController" bundle:nil];
        @KWeakObj(vc)
        vc.orderId = orderId;
        vc.backBtnClick = ^{
            [selfWeak.navigationController popViewControllerAnimated:YES];
        };
        vc.orderBtnClick = ^{
            [vcWeak back];
            OrderXQViewController *vc1 = [[OrderXQViewController alloc] init];
            vc1.oid = orderId;
            vc1.isMyBuy = YES;
            [UIViewController ig_pushViewController:vc1 animated:YES];
        };
        [selfWeak presentViewController:vc animated:YES completion:nil];
    }
    else
    {
        //在这里写支付失败之后的回调操作
        //DeLog(@"支付宝支付失败");
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - 微信支付状态的回调

-(void)dealWXpayResult:(NSNotification*)notification{
    NSString*result=notification.object;
    if([result isEqualToString:@"1"])
    {
        //在这里写支付成功之后的回调操作
        NSLog(@"微信支付成功");
        IGPaymentGoodsResultApi *api = [IGPaymentGoodsResultApi new];
        api.orderId = orderId;
        api.goodsId = _model.goodsId;
        api.payPlatCode = @"wxpay";
        [api startWithDecodeClass:[IGPaymentGoodsResultesp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
            
        }];
        @KWeakObj(self)
        BuySuccViewController *vc = [[BuySuccViewController alloc] initWithNibName:@"BuySuccViewController" bundle:nil];
        @KWeakObj(vc)
        vc.orderId = orderId;
        vc.backBtnClick = ^{
            [selfWeak.navigationController popViewControllerAnimated:YES];
        };
        vc.orderBtnClick = ^{
            [vcWeak back];
            OrderXQViewController *vc1 = [[OrderXQViewController alloc] init];
            vc1.oid = orderId;
            vc1.isMyBuy = YES;
            [UIViewController ig_pushViewController:vc1 animated:YES];
        };
        [selfWeak presentViewController:vc animated:YES completion:nil];
    }else{
        //在这里写支付失败之后的回调操作
        // DeLog(@"微信支付失败");
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
